import axios from 'axios'
export default class FileManage {
    constructor(ip = '') {
        this.http = axios.create({
            baseURL: ip == '' ? '/file/' : `http://${ip}/file/`,
        });
        this.port = null
    }
    interceptors(router) {
        this.http.interceptors.response.use((response) => {
            // 对响应数据做点什么
            //console.warn(response)
            //console.log(response.data.code)
            if (response.data.code == 404) {
                router.push('/')
            }
            return response;
        }, (error) => {
            // 对响应错误做点什么
            router.push('/error-500')
            return Promise.reject(error);
        })
    }
    setPort(port) {
        this.port = port
    }
    list(path) {
        path = '/' + path
        path = path.replace(/\/+/g, '/')
        return this.http.get('list', { params: { path: path, port: this.port } }).then(res => { return res.data })
    }
    pluginlist() {
        return this.http.get('pluginlist', { params: { port: this.port } }).then(res => { return res.data })
    }
    installplugin(filename) {
        return this.http.get('installplugin', { params: { name: filename, port: this.port } }).then(res => { return res.data })
    }
    delplugin(files) {
        files = JSON.stringify(files)
        return this.http.post('delplugin', { files, port: this.port }).then(res => { return res.data })
    }
    cloudpluglist() {
        return this.http.get('cloudpluglist').then(res => { return res.data })
    }
    newdir(path) {
        return this.http.get('newdir', { params: { path } }).then(res => { return res.data })
    }
    rename(oldpath, newpath) {
        return this.http.get('rename', { params: { oldpath, newpath } }).then(res => { return res.data })
    }
    // delpath(path) {
    //     return http.get('delpath', { params: { path } }).then(res=>{return res.data})
    // },
    delfile(path, files) {
        files = JSON.stringify(files)
        return this.http.post('delfile', { path, files, port: this.port }).then(res => { return res.data })
    }
    verifyfile(path) {
        return this.http.get('verifyfile', { params: { path } }).then(res => { return res.data })
    }
    readfile(path) {
        return this.http.get('readfile', { params: { path, port: this.port }, transformResponse: [] }).then(res => { return res.data })
    }
    savefile(path, data) {
        return this.http.post('savefile', { path, data: window.btoa(data) }).then(res => { return res.data })
    }
    upfileplug(files) {
        var forms = new FormData()
        var configs = {
            headers: { 'Content-Type': 'multipart/form-data' }
        };
        forms.append('file', files)
        //console.log(files)
        return this.http.post(`upfileplug?port=${this.port}`, forms, configs).then(res => { return res.data })
    }
    upfile(path, files) {
        var forms = new FormData()
        var configs = {
            headers: { 'Content-Type': 'multipart/form-data' }
        };
        forms.append('file', files)
        //console.log(files)
        return this.http.post(`upfile?path=${path}&port=${this.port}`, forms, configs).then(res => { return res.data })
    }
}